var Sync = new Ext.extend(Ext.Panel, {
	border : false,
	layout : 'fit',
	initComponent : function() {
		this.initGrid();
		this.items = [this.grid];
		Sync.superclass.initComponent.call(this);
	},
	initGrid : function() {
		var me = this;
		var store = new Ext.data.JsonStore({
					fields : ['id', 'tablename', 'chinesename', 'script', {
								name : 'name',
								mapping : 'tablename',
								convert : function(v, r) {
									return r.chinesename + '[' + v + ']';
								}
							}],
					url : My.url('/system/common/queryBySql.do'),
					baseParams : {
						sql : "select * from t_sync where disabled=0"
					},
					root : 'data',
					autoLoad : true
				});
		var sm = new Ext.grid.CheckboxSelectionModel({
					singleSelect : true
				});
		this.grid = new Ext.grid.EditorGridPanel({
			region : 'center',
			boxMinWidth : 480,
			loadMask : true,
			getRecord : function() {
				return me.grid.getSelectionModel().getSelected();
			},
			getRecords : function() {
				return me.grid.getSelectionModel().getSelections();
			},
			store : store,
			viewSync : {
				forceFit : true
			},
			sm : sm,
			cm : new Ext.grid.ColumnModel({
						defaults : {
							align : 'center'
						},
						columns : [new Ext.grid.RowNumberer(), {
									dataIndex : 'name',
									header : '表名',
									width : 300
								}, {
									xtype : 'actioncolumn',
									header : '同步',
									items : [{
										icon : My
												.url('/resources/icon/history.png'),
										tooltip : '同步',
										handler : function(grid, rowIndex,
												colIndex) {
											grid.getSelectionModel()
													.selectRow(rowIndex);
											var rec = store.getAt(rowIndex);
											me.sync(rec);
										}
									}]
								}]
					}),
			border : false,
			listeners : {
				rowclick : function(g, i, e) {
				}
			}
		});
	},
	sync : function() {
		var me = this;
		var recs = me.grid.getRecords();
		if (!recs || recs.length == 0) {
			My.alert('请选择要同步的表 ！');
			return;
		}
		var rec = recs[0];
		var sql = rec.data.script.replace(/\r\n/g, '\n');
		My.confirm('确定要同步表' + rec.data.chinesename + '[' + rec.data.tablename
						+ ']的数据吗？', function(btn) {
					if (btn == 'no') {
						return;
					}
					My.request({
								url : My.url('/system/common/exec.do'),
								params : {
									sql : sql
								},
								successCallback : function() {
									me.grid.getStore().reload();
								}
							});
				});
	}
});

Ext.onReady(function() {
			new Ext.Viewport({
						layout : 'fit',
						items : [new Sync()]
					});
		});